package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.modular.entity.NanSellerAdvert;
import com.tsingtree.modular.entity.NanSellerDevice;
import com.tsingtree.modular.mapper.NanSellerAdvertMapper;
import com.tsingtree.modular.model.params.NanSellerAdvertParam;
import com.tsingtree.modular.model.result.NanSellerAdvertResult;
import  com.tsingtree.modular.service.NanSellerAdvertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.modular.service.NanSellerDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * <p>
 * 运营组-广告栏 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2023-08-29
 */
@Service
public class NanSellerAdvertServiceImpl extends ServiceImpl<NanSellerAdvertMapper, NanSellerAdvert> implements NanSellerAdvertService {

    @Autowired
    private NanSellerDeviceService nanSellerDeviceService;

    @Override
    public void add(NanSellerAdvertParam param){
        NanSellerAdvert entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellerAdvertParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellerAdvertParam param){
        NanSellerAdvert oldEntity = getOldEntity(param);
        NanSellerAdvert newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellerAdvertResult findBySpec(NanSellerAdvertParam param){
        return null;
    }

    @Override
    public List<NanSellerAdvertResult> findListBySpec(NanSellerAdvertParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellerAdvertParam param){
        Page pageContext = getPageContext();
        IPage<NanSellerAdvertResult> page = this.baseMapper.customPageList(pageContext, param);
        List<NanSellerDevice> nanSellerDevices = nanSellerDeviceService.list();
        Map<Integer, NanSellerDevice> map = nanSellerDevices.stream().collect(Collectors.toMap(NanSellerDevice::getId, Function.identity()));
        page.getRecords().forEach(record -> {
            String deviceId = record.getDeviceId();
            if(StringUtils.hasText(deviceId)){
                StringBuilder res = new StringBuilder();
                for (String s : deviceId.split(",")) {
                    Integer id = Integer.parseInt(s.substring(1, s.length()-1));
                    NanSellerDevice nanSellerDevice = map.get(id);
                    res.append(" | ").append(nanSellerDevice.getModel()).append("(").append(nanSellerDevice.getCode()).append(")");
                }
                record.setDeviceName(res.substring(3));
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanSellerAdvertParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellerAdvert getOldEntity(NanSellerAdvertParam param) {
        return this.getById(getKey(param));
    }

    private NanSellerAdvert getEntity(NanSellerAdvertParam param) {
        NanSellerAdvert entity = new NanSellerAdvert();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
